package omg.SongHandling.DataHandling;

import java.io.IOException;
import java.net.HttpURLConnection;
import omg.Error.NoConnectionError;
import omg.Error.StreamCreationFailedError;

public abstract class Stream {
        protected String type;
        private Song song = null;
	private Converter converter = null;

	public Converter getConverter() {
		return this.converter;
	}

	public void setConverter(Converter converter) {
		this.converter = converter;
	}

        /**
	 * Creates a new stream object, ready to stream from the audio source
         * associated with the supplied Song.
         * @throws NoConnectionError Raised if no connection to the other URL
         * could be established.
         * @throws StreamCreationFailedError Raised when the Stream doesn't
         * consider itself responsible for the provided URL - to avoid this,
         * check with the static {@link #isResponsibleFor(Song)}.
         * @param song the Song the stream should be responsible for
	 */
	public Stream(Song song) throws StreamCreationFailedError,
                NoConnectionError {
            try {
                HttpURLConnection urlConn = (HttpURLConnection)song.getLink().
                        toURL().openConnection();
                urlConn.connect();
                if (HttpURLConnection.HTTP_OK != urlConn.getResponseCode())
                    throw new NoConnectionError();
            } catch (IOException e) {
                throw new NoConnectionError();
	    }
            this.song = song;
	}

	/**
	 * Checks whether an this Stream can handle the audio source associated
         * with the supplied song.
         * @param song the Song to check for
	 * @return true, if the parse realizations considers itself responsible
         * for the supplied Song, otherwise false
	 */
	public static boolean isResponsibleFor(Song song) {
		throw new UnsupportedOperationException();
	}

        public String getType() {
            return this.type;
        }

}